<?php 
class SaveResumeModel extends Model{
	public function __construct(){
		$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
	}
	public function queryadvance(){
		$query = "SELECT
					Resumes.id,
					Resumes.rem_name,
					Resumes.rem_city,
					Resumes.rem_code,
					Resumes.rem_position,
					Resumes.rem_experience,
					Resumes.rem_location,
					Resumes.rem_hit,
					Resumes.rem_createDate,
					Resumes.rem_experience_y,
					Resumes.rem_educate
					FROM
					Resumes";
		return  $query;
	}
	
	public function DisplayAdmin($query,$page=1,$perpage=20,$search = array(), $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "$query" ;
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " $key LIKE '%". $val ."%' " ;
			}
			$query .= " WHERE " . implode(" $operation ",$tempSearchFields) . "AND rem_status ='ACT'";
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	
	public function getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria=""){
		$this->open(Model::MYSQL);
		return parent::getList($page,$perpage,$search,$searchFields,$sortFields,$sortCriteria);
	}
	
	public function getResumeInfo($emp_id,$page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria=""){
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$query = " SELECT Resumes.*, SaveResumes.* FROM Resumes,SaveResumes WHERE Resumes.id=SaveResumes.rem_id AND emp_id = $emp_id";
		if(!empty($searchFields)&&(isset($search)&&$search!=="")){
			$tempSearchFields=array();
			foreach($searchFields AS $field ){
				$tempSearchFields[]= " `$field` LIKE '%".$this->stringEscape($search)."%' " ;
			}
			$query .= " AND " . implode(" OR ",$tempSearchFields);
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
			
		}
		$this->open(Model::MYSQL);
		$totalRecords = mysql_num_rows($this->query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;	
		$result = $this->query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	
	public function save($fields=array()){
		$this->open(Model::MYSQL);
		return parent::save($fields);
	}
	
	public function savingg($can_id,$rem_id){
		$this->open(Model::MYSQL);
		$query= "INSERT INTO SaveResumes SET can_id=$can_id,rem_id=$rem_id,save_date=NOW();";
		parent::query($query);
	}
	
	/*
	public function ins($fields=array()){
			$this->open(Model::MYSQL);
			return parent::save("INSERT INTO saveresume values($fields)");
		}
		*/
	public function delete($where=array(),$deleteSet=false){
		$this->open(Model::MYSQL);
		return parent::delete($where,$deleteSet);
	}
	
	public function SelectC(){
		$this->open(Model::MYSQL);
		$selectC=array();
		$result = parent::query("SELECT id,name,parent_id FROM Categories");
		while ($rowsget = mysql_fetch_object($result)){
			$selectC[]=$rowsget;
		}
		return $selectC;
	}
	public function getPage($user_id,$page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT Resumes.rem_name, Resumes.rem_city, Resumes.rem_experience_y, Resumes.rem_educate,
				Resumes.rem_location, Resumes.id, SaveResumes.emp_id FROM Resumes
				INNER JOIN SaveResumes ON Resumes.id = SaveResumes.rem_id Where SaveResumes.emp_id = $user_id" ;
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
}

?>